<?php


require_once ("config/web.init.php");
require_once ("config/web.config.php");
require_once ("base_init.php");
$userLoggedIn = "USER_LOGGED_IN";

$errMsg = "";
$goodMsg = "";
$userX = "";
$passX = "";
$rememberChecked = "";

$homePageFlag = false;

// LOGIN FORM SUBMISSION
if (Security::inputExists()) {
	$userX = Security::inputRead("UserName");
	$passX = Security::inputRead("Password");
	$remember = (Security::inputRead('remember') === 'on') ? true : false;
	if ($remember) {$rememberChecked = "checked";}
	
	$validate = new UserLoginValidator();
	$validation = $validate->check($_POST, array(
			'UserName' => array(
				'required' => true,
				'min' => 2,
				'max' => 100,
				'email' => 'users'),
			'Password' => array(
				'required' => true,
				'min' => 6),
			));
	
		if ($validation->passed()) {
			//print "USER: $userX, PASS: $passX<br/>";
			$umgr = new UserMgr();
			$chkLogin = $umgr->checkLogin($userX, $passX);
			if ($chkLogin) {
				$homePageFlag = true;
				Security::setSession("$userLoggedIn", "$userX");
				if ($remember) {
					Security::setCookie("$userLoggedIn", "$userX", "$cookieTimeout", "/$appRoot");
				}
			} else {
				$errMsg = "ERR: <b>UserName/Password</b> is incorrect!";
			}
		} else {
			foreach($validate->errors() as $error) {
				$errMsg .= "ERR: $error<br/>";
			}
		}
} else if (Security::sessionExists("$userLoggedIn") || Security::cookieExists("$userLoggedIn")) {
		$homePageFlag = true;
	} else {
		include_once("login.php");
	}

if ($homePageFlag) {
	header("Location: homePage.php");
}
?>

<html>
<head>
<title>OGN: Main Page</title>

</head>
<body>

<!-- Main:Table -->
<table border='0' cellspacing='0' cellpadding='0' height='100%' width='100%' valign='center' align='center'>
<!-- Header -->
<tr>
	<td colspan='1' valign='' align='left' width='10%' height='10%'></td>
	<td colspan='1' valign='' align='left' width='80%' height='10%'></td>
	<td colspan='1' valign='' align='left' width='10%' height='10%'></td>
</tr>
<tr>
	<td colspan='1' valign='' align='left' width='10%' height='80%'></td>
	<td colspan='1' valign='center' align='left' width='80%' height='80%'>
	<!-- Center:Table -->
			<table border='0' cellspacing='0' cellpadding='0' height='100%' width='100%' valign='center' align='center'>
				<!-- Header -->
				<tr>
					<td colspan='1' valign='' align='left' width='10%' height='10%'></td>
					<td colspan='1' valign='' align='left' width='80%' height='10%'></td>
					<td colspan='1' valign='' align='left' width='10%' height='10%'></td>
				</tr>
				<tr>
					<td colspan='1' valign='' align='left' width='10%' height='80%'></td>
					<td colspan='1' valign='' align='left' width='80%' height='80%'>
						<!-- Login:Table -->
						<table border='0' cellspacing='0' cellpadding='0' height='100%' width='100%' valign='center' align='center'>
						<!-- Header -->
						<tr>
							<td colspan='1' valign='' align='left' width='23%' height='10%'></td>
							<td colspan='1' valign='' align='left' width='2%' height='10%'></td>
							<td colspan='1' valign='' align='left' width='50%' height='10%'></td>
							<td colspan='1' valign='' align='left' width='25%' height='10%'></td>
						</tr>
						<tr>
							<td colspan='1' valign='' align='left' width='23%' height='10%'></td>
							<td colspan='1' valign='' align='left' width='2%' height='10%'></td>
							<td colspan='1' valign='' align='left' width='50%' height='80%'>
								<div class="container">
								  <div class="login">
									<h3><?php echo $loginTitle;?> :: Login</h3>
									<font color='red'><?php echo $errMsg;?></font>
									  <form name='loginForm' method='post' action=''>
									  <p><input type="text" name="UserName" maxsize='200' size='50' value="<?php echo $userX?>" placeholder="Email"></p>
									  <p><input type="password" name="Password" value="<?php echo $passX?>" placeholder="Password"></p>
									  <p class="remember_me">
										<label>
										 <label>
										  <input type="checkbox" name="remember" id="remember" <?php echo $rememberChecked?>>
										  Remember me on this computer
										</label>
										</label>
									  </p>
									  <p class="submit"><input type="submit" name="submit" value="Login">&nbsp;&nbsp;&nbsp;
<?php 
if ($errMsg === "") {
?>
									  <input type="reset" name="reset" value="Clear">
<?php }?>
									  </p>
									  
									</form>
								  </div>
								 
								  <div class="newuser-help">
									<p>New User <a href="signUp.php">SignUp</a>.</p>
								  </div>
								  <div class="login-help">
									<p>Forgot your password? <a href="forgotPassword.php">Click here to reset...</a>.</p>
								  </div>
								</div>
							</td>
							<td colspan='1' valign='' align='left' width='25%' height='80%'></td>
						</tr>
						<!-- Footer -->
						<tr>
							<td colspan='1' valign='' align='left' width='23%' height='10%'></td>
							<td colspan='1' valign='' align='left' width='2%' height='10%'></td>
							<td colspan='1' valign='' align='left' width='50%' height='10%'></td>
							<td colspan='1' valign='' align='left' width='25%' height='10%'></td>
						</tr>
						</table>
						<!-- Login:Table -->
					</td>
					<td colspan='1' valign='' align='left' width='10%' height='80%'></td>
				</tr>
				<!-- Footer -->
				<tr>
					<td colspan='1' valign='' align='left' width='10%' height='10%'></td>
					<td colspan='1' valign='' align='left' width='80%' height='10%'></td>
					<td colspan='1' valign='' align='left' width='10%' height='10%'></td>
				</tr>
				</table>
			<!-- Center:Table -->
			</td>
			<td colspan='1' valign='' align='left' width='10%' height='80%'></td>
		</tr>
		
		
<!-- Footer -->
<tr>
	<td colspan='1' valign='' align='left' width='10%' height='10%'></td>
	<td colspan='1' valign='' align='left' width='80%' height='10%'>
	<?php include_once("footer.php"); ?>
	</td>
	<td colspan='1' valign='' align='left' width='10%' height='10%'></td>
</tr>
</table>
<!-- Main:Table -->
</body>
</html>
